<?php

// @formatter:off
/**
 * @file switch.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: switch.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/services/mobile/switch.php $
 */
// @formatter:on

class WCServiceMobileSwitch extends WCService {
	//
	// Class methods.
	public function __construct() {
		parent::__construct();

		$this->_requiredParams["params"][] = "turn";

		$this->_useCache = true;
	}
	//
	// Public methods.
	public function check() {
		if(parent::check()) {
			global $wcMVCConfig;

			if(!in_array($this->turn, array("on", "off"))) {
				$this->setError(WC_HTTPERROR_BAD_REQUEST, "Parameter 'turn' can only be 'on' or 'off'.");
			}
			if(isset($wcMVCConfig)) {
				if($wcMVCConfig["skin"] == $wcMVCConfig["skin-mobile"]) {
					$this->setError(WC_HTTPERROR_BAD_REQUEST, "There's no mobile skin configurated.");
				}
			} else {
				$this->setError(WC_HTTPERROR_BAD_REQUEST, "There's no theme configuration.");
			}
		}

		return $this->ok();
	}
	//
	// Protected methods.
	protected function runGET() {
		if($this->turn == "on") {
			$this->_count = (!isset($_COOKIE[WC_COOKIE_MOBILE_MODE]) || $_COOKIE[WC_COOKIE_MOBILE_MODE] != "ON" ? 1 : 0);

			$lifetime = time() + (86400 * 7);
			setcookie(WC_COOKIE_MOBILE_MODE, "ON", $lifetime);
		} else {
			$this->_count = (isset($_COOKIE[WC_COOKIE_MOBILE_MODE]) && $_COOKIE[WC_COOKIE_MOBILE_MODE] == "ON" ? 1 : 0);

			setcookie(WC_COOKIE_MOBILE_MODE, "", 1);
		}
	}
}
